package contato.DAO;

import java.util.ArrayList;
import java.util.Date;

import javax.persistence.EntityManager;
import javax.persistence.Query;


import rta.dataacess.Rastreadores;
import rta.dataacess.Usuario;

public class RastreadorDao extends GenericDAO<Rastreadores> {

	public RastreadorDao() {
		super(Rastreadores.class);

	}

	public RastreadorDao(EntityManager entityManager) {
		super(Rastreadores.class, entityManager);
	}

	public ArrayList<Rastreadores> findbyImei(String imei) {

		ArrayList<Rastreadores> cont = null;

		String hql = "FROM Rastreadores WHERE imei = ?";

		EntityManager entityManager = getEntityManager();
		Query query = entityManager.createQuery(hql);

		query.setParameter(1, imei);

		try {
			cont = (ArrayList<Rastreadores>) query.getResultList();

			return cont;

		} catch (RuntimeException e) {

			return cont;
		}
	}

	public Rastreadores Ultimalocaliza(String imei) {

		Rastreadores rastreadores = null;
		ArrayList<Rastreadores> list;

		String hql = "FROM Rastreadores  ORDER BY id DESC LIMIT 1 ";

		EntityManager entityManager = getEntityManager();
		Query query = entityManager.createQuery(hql);

		list = (ArrayList<Rastreadores>) query.getResultList();

		for (Rastreadores rast : list) {
			if (rast.getImei().equals(imei)) {
				rastreadores = rast;
				break;
			}
		}

		return rastreadores;
	}

	public ArrayList<Rastreadores> findallDate(String rast, Date dataAgora) {
		
		
		ArrayList<Rastreadores> point = null;

		String hql = "FROM Rastreadores WHERE imei = ? AND data = ? ORDER BY hora ASC";

		EntityManager entityManager = getEntityManager();
		Query query = entityManager.createQuery(hql);

		query.setParameter(1, rast);
		query.setParameter(2, dataAgora);

		try {
			point = (ArrayList<Rastreadores>) query.getResultList();

			return point;

		} catch (RuntimeException e) {

			return point;
		}
	}

}
